/**
 * Oscar gonzález 10277
 * Antonio Muñoz
 */
/*
 * Radio_x.java
 * Created on 7/07/2013, 12:59:01 PM
 */
package hoja.pkg1radio;
/** 
 * Descripcion:Clase que sirve como GUI, facilita el uso de la radio.
 * Nombre: Radio_x
 * Parametros: 
 * Valor que retorna:
 * Pre-condiciones:
 * Post-condiciones:
 */
public class Radio_x extends javax.swing.JFrame {
    /* Se inicializan las variables a utilizar en la interfaz*/
    private boolean primer_ON=true;
    private int volumen,x;
    private String nuevo;
    /* Se crea un nuevo objeto de la clase Radio */
    Radio r = new Radio();
    /** 
     * Descripcion: Se inicializan los estados de los distintos botones y elementos
     * a utilizar por el usuario. Esto con el fin de poder tener mayor
     * control de la situacion inicial de la radio.
     * Nombre: Radio_x
     * Parametros: 
     * Valor que retorna:
     * Pre-condiciones:
     * Post-condiciones:
     */
    public Radio_x() {
        initComponents();
        jButton1.setEnabled(false);
        jButton2.setEnabled(false);
        jButton3.setEnabled(false);
        jToggleButton2.setEnabled(false);
        jToggleButton3.setEnabled(false);
        jSlider1.setEnabled(false);
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */

    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        jLabel2 = new javax.swing.JLabel();
        jToggleButton1 = new javax.swing.JToggleButton();
        jSlider1 = new javax.swing.JSlider();
        jButton2 = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();
        jToggleButton3 = new javax.swing.JToggleButton();
        jToggleButton2 = new javax.swing.JToggleButton();
        jProgressBar1 = new javax.swing.JProgressBar();
        jComboBox1 = new javax.swing.JComboBox();
        jLabel1 = new javax.swing.JLabel();
        jButton3 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel2.setFont(new java.awt.Font("AR DESTINE", 2, 36));
        jLabel2.setText("Stand By");

        jToggleButton1.setText("ON/OFF");
        jToggleButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jToggleButton1ActionPerformed(evt);
            }
        });

        jSlider1.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                jSlider1StateChanged(evt);
            }
        });

        jButton2.setText("Seleccionar");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jButton1.setText("Siguiente");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jToggleButton3.setText("FM");
        jToggleButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jToggleButton3ActionPerformed(evt);
            }
        });

        jToggleButton2.setText("AM");
        jToggleButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jToggleButton2ActionPerformed(evt);
            }
        });

        jComboBox1.setFont(new java.awt.Font("Tempus Sans ITC", 1, 12));

        jLabel1.setFont(new java.awt.Font("AR DESTINE", 0, 18));
        jLabel1.setText("Vol.");

        jButton3.setText("Guardar");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 190, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(44, 44, 44)
                                .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                        .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 94, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGap(18, 18, 18)
                                        .addComponent(jButton3, javax.swing.GroupLayout.DEFAULT_SIZE, 141, Short.MAX_VALUE))
                                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                        .addComponent(jToggleButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 76, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGap(66, 66, 66)
                                        .addComponent(jToggleButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 81, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 18, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(jToggleButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 109, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(108, 108, 108)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(jSlider1, javax.swing.GroupLayout.Alignment.LEADING, 0, 0, Short.MAX_VALUE)
                            .addComponent(jProgressBar1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addGap(27, 27, 27)
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 86, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jToggleButton2, javax.swing.GroupLayout.DEFAULT_SIZE, 59, Short.MAX_VALUE)
                        .addComponent(jToggleButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 55, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jToggleButton1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 59, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(31, 31, 31)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jButton3, javax.swing.GroupLayout.DEFAULT_SIZE, 54, Short.MAX_VALUE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, 54, Short.MAX_VALUE)
                        .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, 54, Short.MAX_VALUE)))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jProgressBar1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jSlider1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(16, 16, 16))
        );

        pack();
    }// </editor-fold>                        
    /** 
     * Descripcion: En este boton, se utiliza el metodo Power para cambiar de encendido
     * a apagado, asi tambien se controla la habilitacion o inhabilitacion
     * de los distintos botones que contiene la interfaz.
     * Nombre: jToggleButton1ActionPerformed
     * Parametros: evt (presionar boton)
     * Valor que retorna: 
     * Pre-condiciones: 
     * Post-condiciones: 
     */
    private void jToggleButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                               
        r.Power();
        if ((primer_ON==true) && (r.IsOn()==true)){
            jButton1.setEnabled(true);
            jButton2.setEnabled(true);
            jButton3.setEnabled(true);
            jToggleButton2.setEnabled(true);
            jToggleButton3.setEnabled(true);
            jSlider1.setEnabled(true);
            volumen=jSlider1.getValue();
            jLabel1.setText("Vol"+String.valueOf(volumen/5));
            jProgressBar1.setValue(volumen);

            for (x=0;x<=11;x++){
            jComboBox1.addItem("Ch"+String.valueOf(x+1)+" "+String.valueOf((Math.rint(r.GetFreq()*1000))/1000));
            r.SaveFreq(x);
            }
            if (r.IsAm()==true){
            jLabel2.setText(String.valueOf(r.GetFreq())+" AM");
            jToggleButton2.setSelected(true);
            }
            else {
            jLabel2.setText(String.valueOf(r.GetFreq())+" FM");
            jToggleButton3.setSelected(true);
            }
            r.ChangeSignal();
            for (x=0;x<=11;x++){
            r.SaveFreq(x);
            }
            r.ChangeSignal();
            primer_ON=false;} else if ((r.IsOn()==true) && (primer_ON==false)){
            /* Si la radio se enciende y apaga se conservan las memorias de la
               radio*/
            jButton1.setEnabled(true);
            jButton2.setEnabled(true);
            jButton3.setEnabled(true);
            jToggleButton2.setEnabled(true);
            jToggleButton3.setEnabled(true);
            jSlider1.setEnabled(true);
            volumen=jSlider1.getValue();
            jLabel1.setText("Vol"+String.valueOf(volumen/5));
            jProgressBar1.setValue(volumen);
            if (r.IsAm()==true){
                jToggleButton2.setSelected(true);
                jLabel2.setText(String.valueOf(r.GetFreq())+" AM");
            } else {
                jToggleButton3.setSelected(true);
                jLabel2.setText(String.valueOf((Math.rint(r.GetFreq()*1000))/1000)+" FM");
            }
            for (x=0;x<=11;x++){
                r.ChangeTune(x);
                jComboBox1.addItem("Ch"+String.valueOf(x+1)+" "+String.valueOf((Math.rint(r.GetFreq()*1000))/1000));
            }
        } else if (r.IsOn()==false){
            /*Si la radio esta apagada se dejan los estados iniciales que se 
              definieron */
            jLabel2.setText("Stand By");
            jToggleButton1.setSelected(false);
            jToggleButton2.setSelected(false);
            jToggleButton3.setSelected(false);
            jComboBox1.removeAllItems();
            jButton1.setEnabled(false);
            jButton2.setEnabled(false);
            jButton3.setEnabled(false);
            jToggleButton2.setEnabled(false);
            jToggleButton3.setEnabled(false);
            jSlider1.setEnabled(false);
        }
}                                              
    /** 
     * Descripcion: Sirve para cambiar el volumen de la radio
     * Nombre: jSlider1StateChanged
     * Parametros: evt (cambio de estado)
     * Valor que retorna: 
     * Pre-condiciones: 
     * Post-condiciones: 
     */
    private void jSlider1StateChanged(javax.swing.event.ChangeEvent evt) {                                      
        volumen=jSlider1.getValue();
        jLabel1.setText("Vol"+String.valueOf(volumen/5));
        jProgressBar1.setValue(volumen);
        volumen=1;
}                                     
    /** 
     * Descripcion: Siver para indicar si hay cambio de banda a AM
     * Nombre: Button2ActionPerformed
     * Parametros: 
     * Valor que retorna:
     * Pre-condiciones:
     * Post-condiciones:
     */
    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        r.ChangeTune(jComboBox1.getSelectedIndex());
        if (r.IsAm()==true){
            jLabel2.setText(String.valueOf(r.GetFreq())+" AM");
        } else {
            jLabel2.setText(String.valueOf((Math.rint(r.GetFreq()*1000))/1000)+" FM");
        }
    }                                        
    /** 
     * Descripcion: Sirve para indicar que hubo cambio de frecuencia, sea AM o FM
     * Nombre: jButton1ActionPerformed
     * Parametros: 
     * Valor que retorna:
     * Pre-condiciones:
     * Post-condiciones:
     */
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        r.NextFreq();
        if (r.IsAm()==true) {
            jLabel2.setText(String.valueOf(r.GetFreq())+" AM");
        } else {
            jLabel2.setText(String.valueOf((Math.rint(r.GetFreq()*1000))/1000)+" FM");
        }
}                                        
    /** 
     * Descripcion: Siver para indicar si hay cambio de banda a FM
     * Nombre: jToggleButton3ActionPerformed
     * Parametros: evt (cambio en boton)
     * Valor que retorna:
     * Pre-condiciones:
     * Post-condiciones:
     */
    private void jToggleButton3ActionPerformed(java.awt.event.ActionEvent evt) {                                               
        jToggleButton3.setSelected(true);
        jToggleButton2.setSelected(false);
        r.ChangeSignal();        jComboBox1.removeAllItems();
        jLabel2.setText(String.valueOf(r.GetFreq())+" FM");
        for (x=0;x<=11;x++){
            r.ChangeTune(x);
            jComboBox1.addItem("Ch"+String.valueOf(x+1)+" "+String.valueOf((Math.rint(r.GetFreq()*1000))/1000));
        }
}                                              
    /** 
     * Descripcion: Siver para indicar si hay cambio de banda a AM
     * Nombre: jToggleButton2ActionPerformed
     * Parametros: evt(cambio de estado)
     * Valor que retorna:
     * Pre-condiciones:
     * Post-condiciones:
     */
    private void jToggleButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                               
        // TODO add your handling code here:
        
        jToggleButton2.setSelected(true);
        jToggleButton3.setSelected(false);
        r.ChangeSignal();
        jLabel2.setText(String.valueOf(r.GetFreq())+" AM");
        jComboBox1.removeAllItems();
        for (x=0;x<=11;x++){
            r.ChangeTune(x);
            jComboBox1.addItem("Ch"+String.valueOf(x+1)+" "+String.valueOf((Math.rint(r.GetFreq()*1000))/1000));
            
        }
}                                              
    /** 
     * Descripcion: Sirve para poder guardar en memoria la frecuencia seleccionada.
     * Nombre: jButton3ActionPerformed
     * Parametros: evt (cambio en boton)
     * Valor que retorna:
     * Pre-condiciones:
     * Post-condiciones:
     */
    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // TODO add your handling code here:
        /**/
        r.SaveFreq(jComboBox1.getSelectedIndex());
        jComboBox1.insertItemAt("Ch"+String.valueOf(jComboBox1.getSelectedIndex()+1)+" "+String.valueOf((Math.rint(r.GetFreq()*1000))/1000), jComboBox1.getSelectedIndex());
        jComboBox1.removeItemAt(jComboBox1.getSelectedIndex());
}                                        

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new Radio_x().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify                     
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JProgressBar jProgressBar1;
    private javax.swing.JSlider jSlider1;
    public javax.swing.JToggleButton jToggleButton1;
    private javax.swing.JToggleButton jToggleButton2;
    private javax.swing.JToggleButton jToggleButton3;
    // End of variables declaration                   
}
